<?php
/**
 * 用户返现
 * User: ws
 * Date: 2018/11/2
 * Time: 14:01
 */

namespace app\admin\controller;
use think\Db;
class Rebates   extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';
            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['r.add_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['u.name'] = array('like','%'.trim($key).'%');
            $list = Db::table('je_rebates')->alias('r')->join('je_user u','r.user_id = u.user_id')->join('je_admin_user a','r.admin_userid = a.admin_user_id')->field('u.name,a.name as aname,r.*')->order('rebates_id desc')->where($where)->paginate(input('limit'))->toArray();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function add(){
        if(request()->isPost()){
            $data = $this->request->post();
            unset($data['name']);
            $data['admin_userid'] = $_SESSION['think']['admin_user']['admin_user_id'];
            $data['add_time'] = time();
            if (!db('rebates')->insert($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }

        return view('add');
    }

    function add1(){
        if(request()->isPost()){
            $data = $this->request->post();
            unset($data['name']);
            $data['admin_userid'] = $_SESSION['think']['admin_user']['admin_user_id'];
            $data['add_time'] = time();
            $data['state'] = 0;
            $data['admin_user_id'] = 0;
            if (!db('rebates')->update($data)) {
                return json(array('code'=>0,'msg'=>'更改失败'));
            }
            return json(array('code'=>200,'msg'=>'更改成功'));
        }
        $info = Db::table('je_rebates')->alias('r')->join('je_user u','r.user_id = u.user_id')->field('u.name,r.*')->where('rebates_id', input('id'))->find();

        return view('add1',['list'=>$info]);
    }

    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();
            $data['audit_time'] = time();
            if($data['state']==2){
                $data['admin_user_id'] = $_SESSION['think']['admin_user']['admin_user_id'];
                if(empty($data['remarks'])){  return json(array('code'=>0,'msg'=>'审核失败，请填写拒绝理由'));}
            }elseif($data['state']==1){
                $user = Db::table('je_rebates')->alias('r')->join('je_user u','r.user_id = u.user_id')->field('u.openid,r.money')->where("rebates_id = {$data['rebates_id']}")->find();
                $pay = & load_wechat('Pay');
                $out_refund_no = 'F'.generateOrderNum();
                $result = $pay->transfers($user['openid'], $user['money']*100, $out_refund_no,'返现');

                if($result==false){
                    return json(array('code'=>0,'msg'=>'操作失败，支付失败，原因不明'));
                }
                $data['state'] = 3;
                $data['pay_time'] = $result['payment_time'];
                $data['pay_serial_number'] = $result['payment_no'];
                $data['pay_results'] = '返现成功';
                $data['admin_user_id'] = $_SESSION['think']['admin_user']['admin_user_id'];
            }

            if (!db('rebates')->update($data)) {
                return json(array('code'=>0,'msg'=>'审核失败'));
            }
            return json(array('code'=>200,'msg'=>'审核成功'));
        }


        $info = Db::table('je_rebates')->alias('r')->join('je_user u','r.user_id = u.user_id')->join('je_admin_user a','r.admin_userid = a.admin_user_id')->field('u.name,a.name as aname,r.*')->where('rebates_id', input('id'))->find();
        $info['admin_user_id'] = db('admin_user')->where("admin_user_id = {$info['admin_user_id']}")->value('name');
        return view('edit',['list'=>$info]);
    }


    function index2(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';
            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['r.add_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['u.name'] = array('like','%'.trim($key).'%');
            $list = Db::table('je_rebates')->alias('r')->join('je_user u','r.user_id = u.user_id')->join('je_admin_user a','r.admin_userid = a.admin_user_id')->field('u.name,a.name as aname,r.*')->order('rebates_id desc')->where($where)->paginate(input('limit'))->toArray();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list2');
    }

    function edit2(){
        if(request()->isPost()){
            $data = $this->request->post();
            $data['audit_time'] = time();
            if($data['state']==2){
                $data['admin_user_id'] = $_SESSION['think']['admin_user']['admin_user_id'];
                if(empty($data['remarks'])){  return json(array('code'=>0,'msg'=>'审核失败，请填写拒绝理由'));}
            }elseif($data['state']==1){
                $user = Db::table('je_rebates')->alias('r')->join('je_user u','r.user_id = u.user_id')->field('u.openid,r.money')->where("rebates_id = {$data['rebates_id']}")->find();
                $pay = & load_wechat('Pay');
                $out_refund_no = 'F'.generateOrderNum();
                $result = $pay->transfers($user['openid'], $user['money']*100, $out_refund_no,'返现');

                if($result==false){
                    return json(array('code'=>0,'msg'=>'操作失败，支付失败，原因不明'));
                }
                $data['state'] = 3;
                $data['pay_time'] = $result['payment_time'];
                $data['pay_serial_number'] = $result['payment_no'];
                $data['pay_results'] = '返现成功';
                $data['admin_user_id'] = $_SESSION['think']['admin_user']['admin_user_id'];
            }

            if (!db('rebates')->update($data)) {
                return json(array('code'=>0,'msg'=>'审核失败'));
            }
            return json(array('code'=>200,'msg'=>'审核成功'));
        }


        $info = Db::table('je_rebates')->alias('r')->join('je_user u','r.user_id = u.user_id')->join('je_admin_user a','r.admin_userid = a.admin_user_id')->field('u.name,a.name as aname,r.*')->where('rebates_id', input('id'))->find();
        $info['admin_user_id'] = db('admin_user')->where("admin_user_id = {$info['admin_user_id']}")->value('name');
        return view('edit2',['list'=>$info]);
    }
}